/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */
package org.osami.gateway.mediator.sensedata.internal;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.osami.gateway.sensor.data.SenseData;
import org.osami.gateway.type.Luminosity;
import org.osami.gateway.type.Temperature;
import org.osami.gateway.type.Temperature.Unit;

import fr.liglab.adele.cilia.Data;

/**
 * Transform a {@code SenseData} into a measurement object.
 * 
 * @author bourretp
 */
public class SenseData2ObjectTransformer {

	private static final String GATEWAY_ID = "B1-163";

	/**
	 * Transform the given SenseData instance into a {@code Temperature} or a
	 * {@code Luminosity}.
	 * 
	 * @param data
	 *            input data containing the sense data
	 * @return output data with a a {@code Temperature} or a {@code Luminosity}.
	 */
	public Data process(Data data) {
		SenseData sd = (SenseData) data.getProperty(SenseData.DEFAULT_DATA_KEY);
		Double value = sd.getValue();
		String gatewayId = GATEWAY_ID;
		String location = sd.getLocation();
		String type = sd.getType();
		Object result;
		if ("temperature".equals(type)) {
			// Create a new temperature
			Temperature.Unit unit = Temperature.Unit.valueOf(sd.getUnit());
			result = new Temperature(value, gatewayId, unit, location);
		} else if ("luminosity".equals(type)) {
			// Create a new luminosity
			result = new Luminosity(value, gatewayId, location);
		} else {
			throw new IllegalArgumentException("Unknown sense data type : "
					+ type);
		}
		return new Data(result);
	}
}
